package com.jason.quectel0805.activities;

import android.os.Bundle;
import android.view.View;

import com.jason.quectel0805.R;
import com.jason.quectel0805.fragments.HomeFragment;
import com.jason.quectel0805.fragments.MallFragment;
import com.jason.quectel0805.fragments.MineFragment;
import com.jason.quectel0805.fragments.ShoppingFragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class FragmentActivity extends AppCompatActivity {
    Fragment homeFragment;
    Fragment mallFragment;
    Fragment shoppingFragment;
    Fragment mineFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        homeFragment = new HomeFragment();
        mallFragment = new MallFragment();
        shoppingFragment = new ShoppingFragment();
        mineFragment = new MineFragment();

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_container, homeFragment, "jason_home");
        transaction.add(R.id.fl_container, mallFragment, "jason_mall");
        transaction.add(R.id.fl_container, shoppingFragment, "jason_shopping");
        transaction.add(R.id.fl_container, mineFragment, "jason_mine");
        transaction.show(homeFragment);
        transaction.hide(mallFragment);
        transaction.hide(shoppingFragment);
        transaction.hide(mineFragment);
        transaction.commitAllowingStateLoss();

        //fragment的几种显示方式：add(),replace()--禁止使用,hide(),show()
    }

    public void goMine(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.show(mineFragment);
        transaction.hide(homeFragment);
        transaction.hide(mallFragment);
        transaction.hide(shoppingFragment);
        transaction.commitAllowingStateLoss();
    }

    public void goShopping(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(mineFragment);
        transaction.hide(homeFragment);
        transaction.hide(mallFragment);
        transaction.show(shoppingFragment);
        transaction.commitAllowingStateLoss();
    }

    public void goMall(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(mineFragment);
        transaction.hide(homeFragment);
        transaction.show(mallFragment);
        transaction.hide(shoppingFragment);
        transaction.commitAllowingStateLoss();
    }

    public void goHome(View view) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(mineFragment);
        transaction.show(homeFragment);
        transaction.hide(mallFragment);
        transaction.hide(shoppingFragment);
        transaction.commitAllowingStateLoss();
    }
}